<?php
namespace jy_nongren;
defined('BY_JYA') or exit('error');
/*
	分销佣金
	
*/
class cv extends \plugin{
	/*
		生成佣金
	*/
	function create($order){
		if($order['price']<=0){
			return false;
		}
		$this->order=$order;
		$member=pdo_get('jy_nongren_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
		if(!$member['id']){
			$member=$this->model('model')->upmember($order['mid']);
		}
		$this->create_commission($member,0);
		if(!$member['fromuser']){
			return false;
		}
		$agent=pdo_get('jy_nongren_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['fromuser']));
		if(!$agent['id']){
			return false;
		}
		$this->create_commission($agent,4);
		return true;
	}
	/*
	*/
	function create_commission($member,$type=0){
		if(!$type){
			$cvrate=$this->setting['cv_buyrate'];
		}elseif($type==4){
			$cvrate=$this->setting['cv_fenbuyrate'];
		}
		if(!$cvrate || $cvrate<=0){
			return false;
		}
		$fee=$cvrate*$this->order['price'];
		$data=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$member['mid'],
				'orderid'=>$this->order['id'],
				'msg'=>$this->cvlog_type[$type],
				'type'=>$type,
				'fee'=>$fee,
				'createtime'=>time(),
		);
		pdo_insert('jy_nongren_cvlog',$data);
		pdo_update('jy_nongren_member',array('cv +='=>$fee),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	function up_renwu($post=array(),$type=0){
		$post['mid']=$post['mid']?:$_SESSION['uid'];
		$mids[]=array('mid'=>$post['mid']);
		$msg=$this->cvlog_type[$type];
		if($type==1){
			$cvrate=$this->setting['cv_yangrate'];
			$fee=$cvrate*$post['price'];
		}elseif($type==2){
			$fee=$this->setting['cv_sale'];
		}elseif($type==3){
			$fee=$this->setting['cv_youbuy'];
			$mids=pdo_getall('jy_nongren_haoyou',array('uniacid'=>$_SESSION['uniacid'],'youmid'=>$post['mid']),'mid');
		}elseif($type==5){
			$fee=$this->setting['cv_lookgoods'];
		}elseif($type==6){
			$fee=$this->setting['cv_zhuangoods'];
		}
		if($type==2 || $type==5 || $type==6){
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$isget=$this->pdo_get('cvlog',array('type'=>$type,'createtime >'=>$daytime,'mid'=>$post['mid'],'uniacid'=>$_SESSION['uniacid']));
			if($isget){
				return false;
			}
		}
		$msg.=',获得CV值'.$fee;
		if(!$fee || $fee<=0 || !$mids){
			return false;
		}
		foreach($mids as $v){
			$data=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$v['mid'],
				'msg'=>$msg,
				'type'=>$type,
				'fee'=>$fee,
				'createtime'=>time(),
			);
			pdo_insert('jy_nongren_cvlog',$data);
			pdo_update('jy_nongren_member',array('cv +='=>$fee),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		}
		return $msg;
	}
}